package com.ruoyi.system.xcenter;

import cn.hutool.extra.mail.MailUtil;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.system.domain.DataxXchangeLog;
import com.ruoyi.system.service.IDataxXchangeLogService;
import com.ruoyi.system.xcenter.exception.DataXException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 被动-数据交换
 */
@Slf4j
public abstract class PassiveEndPointAbstract {

    public static final String END_POINT = "END_POINT";
    public static final String WRAPPER_ADAPTER = "WRAPPER_ADAPTER";
    public static final String CONTEXT = "CONTEXT";

    @Autowired
    private IDataxXchangeLogService xchangeLogService;

    /**
     * 终端消费者
     */
    protected Map<String, Object> consumer = new HashMap<>();

    /**
     * 消费者注册
     */
    public void registryEndPoint(IEndPoint endPoint, EPContext ctx, WrapperAdapter wrapperAdapter) {
        consumer.put(END_POINT, endPoint);
        consumer.put(WRAPPER_ADAPTER, wrapperAdapter);
        consumer.put(CONTEXT, ctx);
    }

    /**
     * 被动消费
     */
    protected void consumeEndPoint(Object body, Integer type) {
        if(consumer.isEmpty()){
            return;
        }
        EPContext context = null;
        IEndPoint endPoint;
        try {
            context = (EPContext) consumer.get(CONTEXT);
            endPoint = (IEndPoint) consumer.get(END_POINT);
            WrapperAdapter wrapperAdapter = (WrapperAdapter) consumer.get(WRAPPER_ADAPTER);

            DataxXchangeLog log = new DataxXchangeLog();
            context.setXchangeLog(log);
            context.getXchangeLog().setXchangeStart(new Date());
            context.getXchangeLog().setEndGroupId(context.getDataxEndGroup().getId());

            Object wrapperData = wrapperAdapter.wrapperData(body, context, type);
            context.getXchangeLog().setPlatformEndReq(JSON.marshal(wrapperData));
            Object dataUpload = endPoint.dataUpload(context, wrapperData, type);
            context.getXchangeLog().setPlatformEndRes(JSON.marshal(dataUpload));
            context.getXchangeLog().setXchangeComplete(new Date());
            xchangeLogService.insertDataxXchangeLog(context.getXchangeLog());
        } catch (Exception e) {
            if (context != null) {
                context.getXchangeLog().setErrorLog(e.getMessage());
                if (e instanceof DataXException) {
                    log.error("数据消费异常，{}", e);
                } else if (e instanceof SocketTimeoutException) {
                    String mail = MailUtil.send("941212702@qq.com", "数据交换异常", "信电-捷顺数据交换异常," + ExceptionUtil.getExceptionMessage(e), true);
                    log.info("邮件已发送.{}", mail);
                } else {
                    log.error("数据消费异常，{}", e);
                    context.getXchangeLog().setErrorLog(ExceptionUtil.getExceptionMessage(e));
                }
                xchangeLogService.insertDataxXchangeLog(context.getXchangeLog());

            }
        }

    }


}
